package kr.co.dcinside.agitater.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kr.co.dcinside.agitater.structure.Article;

public abstract class PageReader
{
    abstract void refresh();

    void read( final String location ) throws IOException
    {
        final URL site = new URL( location );
        final BufferedReader in = new BufferedReader( new InputStreamReader(
            site.openStream(), "utf-8" ) );

        final StringBuilder sb = new StringBuilder();
        String inputLine, result;

        while ( ( inputLine = in.readLine() ) != null )
            sb.append( inputLine );
        in.close();

        result = sb.toString();

        final Pattern pattern = Pattern.compile( "<tr\\b[^>]*>(.*?)</tr>" );
        final Pattern titlePattern = Pattern.compile( "<a\\b[^>]*>(.*?)</a>" );
        final Pattern writerPattern = Pattern
            .compile( "<span\\b[^>]*title=\"([^>]*)\"\\b[^>]*>.*?</span>" );
        final Pattern timePattern = Pattern
            .compile( "<span title='\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d'" );
        final Pattern articleLinkPattern = Pattern
            .compile( "<a href=\"/list.php\\?id=[^>]*\"   >" );

        final Matcher matcher = pattern.matcher( result );
        while ( matcher.find() )
        {
            final String chunk = matcher.group();
            String title = null;
            String writer = null;
            String time = null;
            String articleLink = null;
            final Matcher titleMatcher = titlePattern.matcher( chunk );
            final Matcher writerMatcher = writerPattern.matcher( chunk );
            final Matcher timeMatcher = timePattern.matcher( chunk );
            final Matcher articleMatcher = articleLinkPattern.matcher( chunk );

            if ( !titleMatcher.find() ) continue;
            String subChunk = titleMatcher.group();
            try
            {
                title = subChunk.substring( subChunk.indexOf( ">" ) + 1,
                    subChunk.lastIndexOf( "<" ) );
            }
            catch ( final IndexOutOfBoundsException e )
            {
                continue;
            }

            if ( !writerMatcher.find() ) continue;
            subChunk = writerMatcher.group();
            try
            {
                writer = subChunk.split( "\"" )[1];

            }
            catch ( final IndexOutOfBoundsException e )
            {
                continue;
            }
            if ( !timeMatcher.find() ) continue;
            subChunk = timeMatcher.group();
            try
            {
                time = subChunk.split( "'" )[1];
            }
            catch ( final IndexOutOfBoundsException e )
            {
                continue;
            }

            if ( !articleMatcher.find() ) continue;
            subChunk = articleMatcher.group();
            try
            {
                articleLink = subChunk.split( "\"" )[1];
            }
            catch ( final IndexOutOfBoundsException e )
            {
                continue;
            }

            Article.createArticle( title, writer, time, location + articleLink );
            refresh();
        }

    }

}
